//
//  IMFCMPushModels.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/1/27.
//

#import "IMFCMPushModels.h"

#if TARGET_OS_IOS
#import <UIKit/UIKit.h>
#endif

@implementation IMFCMPushConfig

+ (instancetype)defaultConfig {
    IMFCMPushConfig *config = [[IMFCMPushConfig alloc] init];
    config.showNotificationInForeground = YES;
    config.autoHandleNotificationTap = YES;
    config.soundFileName = @"default";
    return config;
}

+ (instancetype)configWithTargetViewControllerClass:(Class)targetClass {
    IMFCMPushConfig *config = [self defaultConfig];
    config.targetViewControllerClass = targetClass;
    return config;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"IMFCMPushConfig{targetClass=%@, showInForeground=%@, autoHandle=%@, sound=%@}", 
            NSStringFromClass(self.targetViewControllerClass), 
            self.showNotificationInForeground ? @"YES" : @"NO",
            self.autoHandleNotificationTap ? @"YES" : @"NO",
            self.soundFileName ?: @"none"];
}

@end

@implementation IMFCMPushResult

+ (instancetype)successWithToken:(NSString *)fcmToken {
    IMFCMPushResult *result = [[IMFCMPushResult alloc] init];
    result->_success = YES;
    result->_fcmToken = [fcmToken copy];
    result->_errorCode = 0;
    result->_errorMessage = nil;
    return result;
}

+ (instancetype)failureWithCode:(NSInteger)errorCode message:(NSString *)errorMessage {
    IMFCMPushResult *result = [[IMFCMPushResult alloc] init];
    result->_success = NO;
    result->_errorCode = errorCode;
    result->_errorMessage = [errorMessage copy];
    result->_fcmToken = nil;
    return result;
}

- (NSString *)description {
    if (self.success) {
        return [NSString stringWithFormat:@"IMFCMPushResult{success=YES, token=%@}", 
                self.fcmToken ?: @"none"];
    } else {
        return [NSString stringWithFormat:@"IMFCMPushResult{success=NO, errorCode=%ld, errorMessage=%@}", 
                (long)self.errorCode, self.errorMessage];
    }
}

@end

@implementation IMFCMNotificationInfo

+ (instancetype)infoWithUserInfo:(NSDictionary *)userInfo source:(NSString *)source {
    IMFCMNotificationInfo *info = [[IMFCMNotificationInfo alloc] init];
    
    // 解析通知内容
    NSDictionary *aps = userInfo[@"aps"];
    if ([aps isKindOfClass:[NSDictionary class]]) {
        NSDictionary *alert = aps[@"alert"];
        if ([alert isKindOfClass:[NSDictionary class]]) {
            info->_title = alert[@"title"];
            info->_body = alert[@"body"];
        } else if ([alert isKindOfClass:[NSString class]]) {
            info->_body = (NSString *)alert;
        }
    }
    
    // 提取自定义数据（排除aps字段）
    NSMutableDictionary *customData = [userInfo mutableCopy];
    [customData removeObjectForKey:@"aps"];
    info->_customData = [customData copy];
    
    info->_source = [source copy];
    info->_receivedTime = [NSDate date];
    
    return info;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"IMFCMNotificationInfo{title=%@, body=%@, source=%@, customData=%@, time=%@}", 
            self.title ?: @"none", 
            self.body ?: @"none", 
            self.source, 
            self.customData ?: @{}, 
            self.receivedTime];
}

@end